﻿using Microsoft.AspNetCore.Mvc;
using BaseGrpcProtocol;
using Nacos.V2;
namespace BaseGrpcClient.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class GreetController : ControllerBase
    {
        private readonly INacosNamingService _nacosNamingService;
        public GreetController(INacosNamingService nacosNamingService)
        {
            _nacosNamingService = nacosNamingService;
        }
        [HttpGet("Option/SayHello")]
        public async Task<HelloReply> SayHelloAsync(string? name, string ip = "localhost", int port = 7263, string scheme = "https")
        {
            var channel = GrpcNacosExtensions.CreateGrpcChannel(ip, port, scheme);
            var client = new Greeter.GreeterClient(channel);
            var req = new HelloRequest() { Name = string.IsNullOrEmpty(name) ? "sunwukong" : name };
            var reply = await client.SayHelloAsync(req);
            return reply;
        }
        [HttpGet("Nacos/SayHello")]
        public async Task<HelloReply> SayHelloNacosAsync(string? name)
        {
            var channel = await _nacosNamingService.GetGrpcChannel("BaseService");
            var client = new Greeter.GreeterClient(channel);
            var req = new HelloRequest() { Name = string.IsNullOrEmpty(name) ? "damahou" : name };
            var reply = await client.SayHelloAsync(req);
            return reply;
        }
    }
}
